﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.JHWorkflowTrans;
using Microhard.Core;

namespace Microhard.WarehouseInsp.Buz.JHWorkflow
{
    public class JHGetConfig : IGetC6WorkflowConfig
    {
        public JHFlowInfoModel GetConfig(string workflowCode)
        {
            C6Entities db = new C6Entities();
            var entity = db.MH_OtherSysWorkflowConfig.SingleOrDefault(s => s.WorkflowTypeCode == workflowCode);
            if (entity != null)
            {
                return new JHFlowInfoModel
                {
                    AppID = entity.AppID,
                    DJSN = entity.DJSN,
                    WorkflowTypeCode = entity.WorkflowTypeCode,
                    DefaultUserLoginCode = entity.DefaultStartWFUserCode
                };
            }
            return null;
        }

        public void RecordSysWFInfo(MH_ThirdSysStartWorkflowModel info)
        {
            MH_ThirdSysStartWorkflow tswf = new MH_ThirdSysStartWorkflow();
            tswf.AppID = info.AppID;
            tswf.ApplierUserLogginCode = info.ApplierUserLogginCode;
            tswf.Bsid = info.Bsid;
            tswf.CreateTime = DateTime.Now;
            tswf.DjSN = info.DjSN;
            tswf.InstanceID = info.InstanceID;
            tswf.StartAppDID = info.StartAppDID;
            tswf.strUrl = info.strUrl;
            tswf.SyncStatus = false;
            tswf.UserName = info.UserName;
            tswf.Version = info.Version;
            tswf.WorkflowCode = info.WorkflowCode;
            tswf.BuzDataID = info.BuzDataID;
            try
            {
                C6Entities db = new C6Entities();
                db.MH_ThirdSysStartWorkflow.AddObject(tswf);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("RecordSysWFInfo", ex);
            }
        }


        public List<MH_ThirdSysStartWorkflowModel> GetNeedSyncWorkflowInfo()
        {
            C6Entities db = new C6Entities();
            //获得未同步的流程数据
            return (from p in db.MH_ThirdSysStartWorkflow
                    where p.SyncStatus == false
                    select new MH_ThirdSysStartWorkflowModel
                    {
                        AppID = p.AppID,
                        ApplierUserLogginCode = p.ApplierUserLogginCode,
                        Bsid = p.Bsid,
                        CreateTime = p.CreateTime.Value,
                        DjSN = p.DjSN,
                        InstanceID = p.InstanceID,
                        StartAppDID = p.StartAppDID,
                        strUrl = p.strUrl,
                        SyncStatus = p.SyncStatus.Value,
                        UserName = p.UserName,
                        Version = p.Version,
                        WorkflowCode = p.WorkflowCode,
                        BuzDataID = p.BuzDataID.Value,
                        FWID = p.FWID
                    }).ToList();
        }


        public void UpdateWorkflowInfoStatus(long id, string strApproveinfo, bool finApprovedStatus)
        {
            C6Entities db = new C6Entities();
            var entity = db.MH_ThirdSysStartWorkflow.SingleOrDefault(w => w.FWID == id);
            if (entity != null)
            {
                entity.SyncStatus = true;
                entity.ApproveInfo = strApproveinfo;
                entity.ApproveStatus = finApprovedStatus;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("UpdateWorkflowInfoStatus", ex);
            }
        }
    }
}
